﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using System.Web.Routing;

namespace System.Web.Mvc
{
    public static class HtmlHelperExt
    {
        public static MvcHtmlString CheckBox(this HtmlHelper helper, string name,
            object value, bool isChecked)
        {
            return CheckBox(helper, name, value, isChecked, null);
        }

        public static MvcHtmlString CheckBox(this HtmlHelper helper, string name,
            object value, bool isChecked, IDictionary<string, object> attrs)
        {
            TagBuilder builder = new TagBuilder("input");
            if (attrs != null)
            {
                foreach (string key in attrs.Keys)
                {
                    builder.MergeAttribute(key, attrs[key] != null ? attrs[key].ToString() : null);
                }
            }
            builder.MergeAttribute("type", "checkbox");
            builder.MergeAttribute("name", name);
            if (isChecked)
                builder.MergeAttribute("checked", "checked");
            builder.MergeAttribute("value", value != null ? value.ToString() : string.Empty);
            if (!builder.Attributes.ContainsKey("id"))
            {
                builder.MergeAttribute("id", name);
            }
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
        }

        public static MvcHtmlString CheckBox(this HtmlHelper helper, string name, object value,
            bool isChecked, object htmlAttributes)
        {
            Dictionary<string, object> attrs = new Dictionary<string, object>();
            foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(htmlAttributes))
            {
                object o = property.GetValue(htmlAttributes);
                attrs.Add(property.Name, o != null ? o.ToString() : null);
            }
            return CheckBox(helper, name, value, isChecked, attrs);
        }

        public static MvcHtmlString CultureLink(this HtmlHelper helper, string cultureName,
            string linkText, IDictionary<string, object> htmlAttributes)
        {
            var routeValues = helper.ViewContext.RouteData.Values;
            var routeCollection = new RouteValueDictionary(routeValues);
            var querys = helper.ViewContext.HttpContext.Request.QueryString;
            foreach (string queryKey in querys)
            {
                if (string.IsNullOrWhiteSpace(queryKey) || querys[queryKey] == null) continue;
                if (routeCollection.ContainsKey(queryKey))
                    routeCollection[queryKey] = querys[queryKey];
                else
                    routeCollection.Add(queryKey, querys[queryKey]);
            }
            routeCollection["lang"] = cultureName;
            var urlHelper = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection);
            var cultureUrl = urlHelper.RouteUrl(routeCollection);
            bool isSelected = Thread.CurrentThread.CurrentUICulture.Name.ToLower() == cultureName.ToLower();

            TagBuilder builder = new TagBuilder("a");
            if (htmlAttributes != null)
            {
                string classStr = htmlAttributes.ContainsKey("class")
                                      ? htmlAttributes["class"].ToString()
                                      : string.Empty;
                if (isSelected)
                {
                    classStr = "currLang;" + classStr;
                    htmlAttributes["class"] = classStr;
                }
                foreach (string attrKey in htmlAttributes.Keys)
                {
                    builder.MergeAttribute(attrKey,
                                           htmlAttributes[attrKey] != null ? htmlAttributes[attrKey].ToString() : null);
                }
            }
            builder.MergeAttribute("href", cultureUrl);
            builder.MergeAttribute("title", linkText);
            builder.SetInnerText(linkText);
            return MvcHtmlString.Create(builder.ToString());
        }
    }
}